package com.hp.controller;

import com.hp.bean.Customerservice;
import com.hp.bean.CustomerserviceExample;
import com.hp.bean.Master;
import com.hp.bean.MasterExample;
import com.hp.service.CustomerService;
import com.hp.service.CustomerserviceService;
import com.hp.service.MasterService;
import com.hp.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private CustomerserviceService service;
    @Resource
    RedisUtil redisUtil;
    @Resource
    CustomerService customerService;

    @Resource
    private MasterService masterService;


    @RequestMapping("/login")
    @ResponseBody
    public Object customerservicelogin(Customerservice customerservice,String code, HttpSession session) {
        Map<String,Object> map = new HashMap<>();
        String codeStr = (String) session.getAttribute("code");
        if (codeStr == null || !codeStr.equalsIgnoreCase(code)) {
            map.put("type", "error");
            map.put("msg", "验证码错误！");
        }
        CustomerserviceExample example = new CustomerserviceExample();
        Customerservice cs = service.login(example);
        System.out.println(cs);
        if(cs!=null){
            session.setAttribute("customerservice", cs);
            map.put("type", "success");
            map.put("msg", "登录成功！");
        }else{
            map.put("type", "error");
            map.put("msg", "登录失败！");
        }
        return map;
    }


    @RequestMapping("/tocs")
    public String tocs(){
        return "/view/admin/login.html";
    }
    @RequestMapping("/csindex")
    public String csindex(){
        return "/view/admin/admin.jsp";
    }



    //跳转到顾客登录页面
    @RequestMapping("/customerPhone")
    public String customerPhone(){
        return "/view/customerPhone/html/login.html";
    }

    @RequestMapping("/phoneindex")
    public String index(){
        return "/view/customerPhone/index.html";
    }

    @RequestMapping("/phonelogin")
    @ResponseBody
    public JsonData login(@RequestParam Map map, HttpSession session){
        String phone = map.get("phone")==null?"":map.get("phone").toString();
        String code = map.get("code")==null?"":map.get("code").toString();

        if (!redisUtil.hasKey(phone)){
            return JsonData.buildError("验证码不存在！");
        }
        String code1 =(String) redisUtil.get(phone);
        if (!code1.equalsIgnoreCase(code)){
            return JsonData.buildError("验证码错误！");
        }

        //开启一个线程，避免影响用户清除的速度
        new Thread(){
            @Override
            public void run() {
                customerService.login(phone);
            }
        }.start();

        session.setAttribute("phone",phone);
        return JsonData.buildSuccess("成功！");
    }

    /**
     *
     * @return
     */

    @RequestMapping("/sendSms")
    @ResponseBody
    public String sendSms(String phone) throws Exception {
        //如果缓存还存在，那么不发送短信，让验证码在控制台输出
        System.out.println(redisUtil.hasKey(phone));
        if (!redisUtil.hasKey(phone)){
            Map<String,Object> map = new HashMap<>();
            map.put("code", DataUtils.getVerificationCode(6));
            map.put("phone",phone);
            //调用短信方法
            Sample.smsCode(map);
            redisUtil.set(phone,(String) map.get("code"),24*60*60);

            return "短信发送成功";
        }
        System.out.println(redisUtil.get(phone));

        return "验证码还在有效期";

    }

    @RequestMapping("/getSession")
    public String getSession(HttpSession session){
        String phone = (String) session.getAttribute("phone");
        if (phone!=null){
            return "success";
        }
        return "error";
    }

    /**
     * 计算金额的方法
     */
    @RequestMapping(value = "/getMoney")
    @ResponseBody
    public String getMoney(String lng,String lat){
        String lng1 = "113.838124";
        String lat1= "34.729147";
        int i = (int) MapUtils.algorithm(Double.valueOf(lng1),Double.valueOf(lat1),Double.valueOf(lng),Double.valueOf(lat))/100;
        return i+"";
    }

    @RequestMapping("/masterlogin")
    @ResponseBody
    public Object masterlogin(Master master, String code, HttpSession session) {
        Map<String,Object> map = new HashMap<>();
        String codeStr = (String) session.getAttribute("code");
        if (codeStr == null || !codeStr.equalsIgnoreCase(code)) {
            map.put("type", "error");
            map.put("msg", "验证码错误！");
        }
        MasterExample example = new MasterExample();
        Master cs = masterService.login(example);
        System.out.println(cs);
        if(cs!=null){
            session.setAttribute("customerservice", cs);
            map.put("type", "success");
            map.put("msg", "登录成功！");
        }else{
            map.put("type", "error");
            map.put("msg", "登录失败！");
        }
        return map;
    }

    @RequestMapping("/masterindex")
    public String masterindex(){
        return "/view/master/admin.jsp";
    }

}
